//
//  CFDictionary.swift
//  
//
//  Created by tangbl93 on 2021/3/27.
//

import CoreFoundation

// MARK: yx
public extension CFDictionary {
    
    // valueforKey CFString
    func yx_value(forKey key: CFString) -> AnyObject? {
        guard let opaqueValue = CFDictionaryGetValue(self, Unmanaged.passUnretained(key).toOpaque()) else {
            return nil
        }
        let unretainedValue = Unmanaged<AnyObject>.fromOpaque(opaqueValue).takeUnretainedValue()
        return unretainedValue
    }
}

// MARK: subscript
public extension CFDictionary {
    
    @inlinable subscript(key: CFString) -> AnyObject? {
        return yx_value(forKey: key)
    }
    
    @inlinable subscript(key: CFString, default defaultValue: AnyObject) -> AnyObject {
        return yx_value(forKey: key) ?? defaultValue
    }
}
